﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using AMS.DataAccess.Common;
using System.Data;
using AMS.DataAccess.Sys.Interfaces;

namespace AMS.DataAccess.Sys
{
    /// <summary>
    /// 关于 Index 的构造，预留的参数可以有：
    /// %Date%: 表示日期
    /// </summary>
    public class IndexPrivider : IIndexProvider
    {
        public string NextTrademarkCaseIndex()
        {
            return NextIndex("tmcase");
        }

        public string NextIndex(string category)
        {
            SqlParameter []paras = new SqlParameter[]{
                new SqlParameter("@moduleId", category)
            };

            string format = null;
            IDataReader dr = SqlDBHelper.RunProcedure("pr_NextIndex", paras);
            try
            {
                if (dr.Read())
                {
                    format = AdoConverter.ToString(dr[0]);
                }
            }
            finally
            {
                dr.Close();
            }

            if (!string.IsNullOrEmpty(format))
            {
                int index = format.IndexOf("%Date%", StringComparison.InvariantCultureIgnoreCase);
                if (index >= 0)
                {
                    format = format.Replace("%Date%", DateTime.Now.ToString("yyyyMMddHHmm"));
                }

                format = format.Replace("%%", "%");
            }

            return format;
        }
    }
}
